package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.dto.inventory;
import com.hmall.feignconfig.AddressClient;
import com.hmall.feignconfig.itemClient;
import com.hmall.order.interceptor.ThreadLocals;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.*;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    private itemClient client;

    @Autowired
    private AddressClient addressClient;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Override
    @Transactional
    public Long order(CreateOrder createOrder) {

        //用户Id
        Long userId = (Long) ThreadLocals.getthread().get();

        // 生成雪花Id
        long id = IdWorker.getId();

        // 商品ID
        Long commodityId =  createOrder.getItemId();

        // 根据商品Id 查询这条数据
        Item byId = client.getById(commodityId);

        // 一个商品的单价
        Long price = byId.getPrice();
        // 商品的数量
        Integer num = createOrder.getNum();
        // 总价
        Long totalFee = price*num;

        Order order = new Order();
        // 订单Id
        order.setId(id);
        // 商品金额
        // 支付的方式
        order.setPaymentType(createOrder.getPaymentType());
        order.setTotalFee(totalFee);
        // 付款方式：1:微信支付, 2:支付宝支付, 3:扣减余额
        order.setStatus(1);
        // 用户Id
        order.setUserId(userId);
        order.setCreateTime(new Date());
        // 写入数据库tb_order表中
        orderMapper.insert(order);

        OrderDetail detail = new OrderDetail();
        // 订单Id
        detail.setOrderId(id);
        // 商品Id
        detail.setItemId(commodityId);
        // 购买商品数量
        detail.setNum(num);
        // 商品标题  名字
        detail.setName(byId.getName());
        // 单价
        detail.setPrice(price);
        //商品规格
        detail.setSpec(byId.getSpec());
        // 图片
        detail.setImage(byId.getImage());
        // 创建时间
        detail.setCreateTime(new Date());
        // 将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        orderDetailMapper.insert(detail);

        // 根据地址ID 拿到地址数据
        Address addressById = addressClient.findAddressById(createOrder.getAddressId());

        // 封装地址对象
        OrderLogistics logistics = new OrderLogistics();
        // 订单ID
        logistics.setOrderId(id);
        // 收件人
        logistics.setContact(addressById.getContact());
        // 手机号
        logistics.setMobile(addressById.getMobile());
        // 省
        logistics.setProvince(addressById.getProvince());
        // 市
        logistics.setCity(addressById.getCity());
        // 区
        logistics.setTown(addressById.getTown());
        // 街道
        logistics.setStreet(addressById.getStreet());
        // 创建时间
        logistics.setCreateTime(new Date());
        // 将地址封装为OrderLogistics对象，写入tb_order_logistics表
        orderLogisticsMapper.insert(logistics);

        // 减库存
        client.stock(commodityId,num);

        return id;
    }


}
